﻿using AnQinManagermentBackendAPI.Common;
using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.DevelopManagement;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.DevelopManagement;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class DevelopManagementService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public DevelopManagementService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取开发管理列表
         */
        public async Task<RequestResponseModel<PageResult<DevelopManagementView>>> GetDevelopManagementList(DevelopManagementSearchDTO developManagementSearchDTO)
        {
            //筛选
            var query = _context.DevelopManagementView
                .WhereIF(a => a.Code.Contains(developManagementSearchDTO.Code), !string.IsNullOrEmpty(developManagementSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(developManagementSearchDTO.Name), !string.IsNullOrEmpty(developManagementSearchDTO.Name))
                .WhereIF(a => a.StaffId.Contains(developManagementSearchDTO.StaffId),!string.IsNullOrEmpty(developManagementSearchDTO.StaffId))
                .WhereIF(a => a.ProjectId.Contains(developManagementSearchDTO.ProjectId),!string.IsNullOrEmpty(developManagementSearchDTO.ProjectId))
                ;

            //返回列表
            var developManagement = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(developManagementSearchDTO.CurrentPage, developManagementSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<DevelopManagementView>(developManagement, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增开发管理
         */
        public async Task<RequestResponseModel<string>> AddDevelopManagement(DevelopManagementAddDTO developManagementAddDTO)
        {
            //操作数据
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var developManagement = new DevelopManagementModel
            {
                Id = guid.ToString(),
                Code = developManagementAddDTO.Code,
                Name = developManagementAddDTO.Name,
                StaffId = developManagementAddDTO.StaffId,
                ProjectId = developManagementAddDTO.ProjectId,
                State = StateEnum.DevelopStateEnum.待开发,
                File = developManagementAddDTO.File,
                Remark = developManagementAddDTO.Remark,
                StartTime = developManagementAddDTO.StartTime,
                EndTime = developManagementAddDTO.EndTime,
                CreateTime = date,
            };

            //新增开发管理
            _context.DevelopManagement.Add(developManagement);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 更新开发管理
         */
        public async Task<RequestResponseModel<string>> PutDevelopManagement(DevelopManagementUpdateDTO developManagementUpdateDTO)
        {
            //查找数据
            var query = await _context.DevelopManagement.FindAsync(developManagementUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", developManagementUpdateDTO.Id);
            };

            //生成新数据
            query.Name = developManagementUpdateDTO.Name;
            query.StaffId = developManagementUpdateDTO.StaffId;
            query.ProjectId = developManagementUpdateDTO.ProjectId;
            query.File = developManagementUpdateDTO.File;
            query.Remark = developManagementUpdateDTO.Remark;
            query.StartTime = developManagementUpdateDTO.StartTime;
            query.EndTime = developManagementUpdateDTO.EndTime;

            //更新开发管理表
            _context.DevelopManagement.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", developManagementUpdateDTO.Id);
        }

        /**
         * 删除开发管理
         */
        public async Task<RequestResponseModel<string>> DeleteDevelopManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.DevelopManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.DevelopManagement.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 开发开始
         */
        public async Task<RequestResponseModel<string>> StartDevelopManagement(DevelopManagementPlanDTO developManagementPlanDTO)
        {
            //校验Id有效性
            var query = await _context.DevelopManagement.FindAsync(developManagementPlanDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", developManagementPlanDTO.Id);
            };

            //更改状态
            query.State = StateEnum.DevelopStateEnum.开发中;
            query.PlanStartTime = developManagementPlanDTO.PlanStartTime;
            query.PlanEndTime = developManagementPlanDTO.PlanEndTime;

            //保存库
            _context.DevelopManagement.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "开始成功", developManagementPlanDTO.Id);
        }

        /**
         * 更改计划时间
         */
        public async Task<RequestResponseModel<string>> UpdatePlanTimeDevelopManagement(DevelopManagementPlanDTO developManagementPlanDTO)
        {
            //校验Id有效性
            var query = await _context.DevelopManagement.FindAsync(developManagementPlanDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", developManagementPlanDTO.Id);
            };

            //更改状态
            query.PlanStartTime = developManagementPlanDTO.PlanStartTime;
            query.PlanEndTime = developManagementPlanDTO.PlanEndTime;

            //保存库
            _context.DevelopManagement.Update(query);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更改成功", developManagementPlanDTO.Id);
        }

        /**
         * 开发完成
         */
        public async Task<RequestResponseModel<string>> FinishDevelopManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.DevelopManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //查找项目表
            var project = await _context.Project.FindAsync(query.ProjectId);

            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "项目未找到", Id);
            }

            //更改数据
            query.State = StateEnum.DevelopStateEnum.已完成;
            project.State = StateEnum.ProjectStateEnum.待实施;

            //保存库
            _context.DevelopManagement.Update(query);
            _context.Project.Update(project);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "完成成功", Id);
        }
    }
}
